#include "myserver.h"
#include<QFile>
MyServer::MyServer(QTcpSocket *socket1,QObject *parent) : QThread(parent) ,socket(socket1)
{

}

void MyServer::run()
{
    QFile *file=new QFile("recv.txt");
    file->open(QIODevice::WriteOnly);


    connect(socket,&QTcpSocket::readyRead,this,[=](){
        static int count=0;
        static int total=0;
        if(count==0)
        {
            socket->read((char*)&total,4);
        }

        QByteArray all=socket->readAll();
        count+=all.size();
        file->write(all);

        if(count==total)
        {
            socket->close();
            socket->deleteLater();
            file->deleteLater();
            emit over();
        }
    });
    exec();
}
